import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_turbolink_attribution/flutter_turbolink_attribution.dart';
import 'package:flutter_turbolink_attribution/turbolink_deeplink_properties.dart';
import 'package:get/get.dart';
import 'package:sinosheets/network/api.dart';
import 'package:sinosheets/utils/bus_utils.dart';
import 'package:sinosheets/utils/lottie_utils.dart';
import 'package:sinosheets/utils/storage/LSUtil.dart';

class AddCodeLogic extends GetxController {
  final TextEditingController inviteCodeController = TextEditingController();
  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    inviteCodeController.text = LSUtil.getInviteCode()['code'] ?? '';
    update();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  void copyInviteLink() {
    TurboLinkDeepLinkProperties properties = TurboLinkDeepLinkProperties();
    properties.setTitle("Promo Code").setTemplateId("ux2F").setUserId(LSUtil.getInviteCode()['code'] ?? '').setLinkData({"code": LSUtil.getInviteCode()['code'] ?? ''}).setDefaultRedirection("https://www.sinosheets.com");
    TurbolinkSdk.createDeepLink(properties, (data) {
      print("callback:${data.shortHash}, ${data.linkUrl}, ${data.linkTemplate}");
      Clipboard.setData(ClipboardData(text: data.linkUrl));
    });
  }

  void dealInviteCode() {
    LottieUtils.showToastLoading();
    ResourceApis.validateInviteCode(inviteCodeController.text).then((value) {
      if (value.isSuccess && value.data == true) {
        LSUtil.saveInviteCode(inviteCodeController.text, true);
        Get.back();
        bus.emit('code_update');
      } else {
        LSUtil.saveInviteCode(inviteCodeController.text, false);
        Get.back();
        bus.emit('code_update');
      }
      LottieUtils.hideToastLoading();
    });
  }
}
